import { IInterrupt } from "../api/interrupt";
import { IMapper } from "../api/mapper";
import { uint16 } from "../api/types";

class Mapper0 implements IMapper {
    private readonly isMirrored: boolean
    interrupt!: IInterrupt;

    constructor(
        private readonly ram: Uint8Array,
        private readonly prg: Uint8Array,
        private readonly chr: Uint8Array,
    ) {
        this.isMirrored = prg.length == 16 * 1024
    }
    ppuClockHandle(scanLine: number, cycle: number): void {
        throw new Error("Method not implemented.");
    }

    read(address: number): number {
        if (address >= 0x8000) {
            return this.prg[this.parseAddress(address)]
        }
        return 0;
    }
    write(address: number, data: number): void {
        address &= 0xFFFF;
        // 写入数据
        if (address >= 0x8000) {
            this.prg[this.parseAddress(address)] = data;
        }
    }

    private parseAddress(address: uint16) {
        if (address < 0x2000) {
            return address
        } else {
            return (this.isMirrored
                ? address & 0b1011111111111111
                : address
            ) - 0x8000
        }
    }

}

export {
    Mapper0
}